<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;

class Index extends Controller
{
    public function index()
    {
		$this->assign([
			'time'=>date("Y-m-d H:i:s")
		]);
        return $this->fetch();
    }

    /**
     * 计算概率
     */
    public function getIsMarry(){
    	$param = input('post.');
    	$param = $this->val2step($param);
    	$count = DB::table('is_marry')->count();
    	if($count == 0){
    		return ['code'=>'-1','msg'=>'请添加数据'];
    	}
    	$bP[0] = DB::table('is_marry')
    			->where('handsome',$param['handsome'])
    			->count() / $count;
    	$bP[1] = DB::table('is_marry')
    			->where('female_tall_step',$param['female_tall_step'])
    			->count() / $count;
    	$bP[2] = DB::table('is_marry')
    		->where('female_income_step',$param['female_income_step'])
    		->count() / $count;
    	$bP[3] = DB::table('is_marry')
    			->where('house',$param['house'])
    			->count() / $count;
    	$bP[4] = DB::table('is_marry')
    			->where('car',$param['car'])
    			->count() / $count;
    	$bP[5] = DB::table('is_marry')
    			->where('kind',$param['kind'])
    			->count() / $count;
    	$bP[6] = DB::table('is_marry')
    			->where('industrious',$param['industrious'])
    			->count() / $count;
    	$bP[7] = DB::table('is_marry')
    			->where('tall_step',$param['tall_step'])
    			->count() / $count;
    	$bP[8] = DB::table('is_marry')
    			->where('weight_step',$param['weight_step'])
    			->count() / $count;
    	$bP[9] = DB::table('is_marry')
    			->where('income_step',$param['income_step'])
    			->count() / $count;
    	#-----------------------------------
    	$Married = DB::table('is_marry')->where('married',1)->count() / $count;

    	#------------------------------------
    	$MP[0] = DB::table('is_marry')
    			->where('married',1)
    			->where('handsome',$param['handsome'])
    			->count() / $count;
    	$MP[1] = DB::table('is_marry')
    			->where('married',1)
    			->where('female_tall_step',$param['female_tall_step'])
    			->count() / $count;
    	$MP[2] = DB::table('is_marry')
    			->where('married',1)
    		->where('female_income_step',$param['female_income_step'])
    		->count() / $count;
    	$MP[3] = DB::table('is_marry')
    			->where('married',1)
    			->where('house',$param['house'])
    			->count() / $count;
    	$MP[4] = DB::table('is_marry')
    			->where('married',1)
    			->where('car',$param['car'])
    			->count() / $count;
    	$MP[5] = DB::table('is_marry')
    			->where('married',1)
    			->where('kind',$param['kind'])
    			->count() / $count;
    	$MP[6] = DB::table('is_marry')
    			->where('married',1)
    			->where('industrious',$param['industrious'])
    			->count() / $count;
    	$MP[7] = DB::table('is_marry')
    			->where('married',1)
    			->where('tall_step',$param['tall_step'])
    			->count() / $count;
    	$MP[8] = DB::table('is_marry')
    			->where('married',1)
    			->where('weight_step',$param['weight_step'])
    			->count() / $count;
    	$MP[9] = DB::table('is_marry')
    			->where('married',1)
    			->where('income_step',$param['income_step'])
    			->count() / $count;

    	#------------------------------------------------
    	$NP[0] = DB::table('is_marry')
    			->where('married',0)
    			->where('handsome',$param['handsome'])
    			->count() / $count;
    	$NP[1] = DB::table('is_marry')
    			->where('married',0)
    			->where('female_tall_step',$param['female_tall_step'])
    			->count() / $count;
    	$NP[2] = DB::table('is_marry')
    			->where('married',0)
    		->where('female_income_step',$param['female_income_step'])
    		->count() / $count;
    	$NP[3] = DB::table('is_marry')
    			->where('married',0)
    			->where('house',$param['house'])
    			->count() / $count;
    	$NP[4] = DB::table('is_marry')
    			->where('married',0)
    			->where('car',$param['car'])
    			->count() / $count;
    	$NP[5] = DB::table('is_marry')
    			->where('married',0)
    			->where('kind',$param['kind'])
    			->count() / $count;
    	$NP[6] = DB::table('is_marry')
    			->where('married',0)
    			->where('industrious',$param['industrious'])
    			->count() / $count;
    	$NP[7] = DB::table('is_marry')
    			->where('married',0)
    			->where('tall_step',$param['tall_step'])
    			->count() / $count;
    	$NP[8] = DB::table('is_marry')
    			->where('married',0)
    			->where('weight_step',$param['weight_step'])
    			->count() / $count;
    	$NP[9] = DB::table('is_marry')
    			->where('married',0)
    			->where('income_step',$param['income_step'])
    			->count() / $count;
    	#------------------------------------------------
    	$P_A = $Married;
    	$P_B = 1;
    	$P_BA = 1;
    	$P_BnotA = 1;
    	foreach($bP as $v){
    		$P_B *= $v;
    	}
    	foreach($MP as $v){
    		$P_BA *= $v;
    	}
    	foreach($NP as $v){
    		$P_BnotA *= $v;
    	}

    	if($P_B == 0){
    		$P_B = 1 / $count;
    	}
    	$P_A_B = $P_BA * $P_A / $P_B;
    	$P_notA_B = $P_BnotA * (1-$P_A) / $P_B;
    	if($P_A_B > $P_notA_B){
    		$suggest = "建议尝试一下哦";
    		$code = 1;
    	}else{
    		$suggest = "还是先改善一下自己吧";
    		$code = 0;
    	}
    	$res = sprintf("%.6f",$P_A_B * 100);
    	$data = [
    		"PA" => sprintf("%.6f",$P_A*100),
    		"PBA" => sprintf("%.8f",$P_BA*100),
    		"PB" => sprintf("%.6f",$P_B*100),
    		"PnotAB" => sprintf("%.6f",$P_notA_B*100),
    	];
    	$ps = "<span style='color:red'>注:</span>"
    		. "暂时没有加入权重，所以概率会比较小，"
    		. "数据模型纯随机没有参考性，机器学习学出来也是个智障=.=";
    	return ['code'=>$code,'msg'=>$res,'data'=>$data,'ps'=>$ps,'suggest'=>$suggest];
    }

    public function val2step($param){
    	// tall_step
    	if($param['tall'] < 160){
    		$param['tall_step'] = 1;
    	}elseif($param['tall'] >= 160 && $param['tall']<165){
    		$param['tall_step'] = 2;
    	}elseif($param['tall'] >= 166 && $param['tall']<170){
    		$param['tall_step'] = 3;
    	}elseif($param['tall'] >= 170 && $param['tall']<175){
    		$param['tall_step'] = 4;
    	}elseif($param['tall'] >= 175 && $param['tall']<180){
    		$param['tall_step'] = 5;
    	}else{
    		$param['tall_step'] = 6;
    	}
    	// weight_step
    	if($param['weight'] < 60){
    		$param['weight_step'] = 1;
    	}elseif($param['weight']<70 && $param['weight']>=60){
    		$param['weight_step'] = 2;
    	}elseif($param['weight']<80 && $param['weight']>=70){
    		$param['weight_step'] = 3;
    	}elseif($param['weight']<90 && $param['weight']>=80){
    		$param['weight_step'] = 4;
    	}elseif($param['weight']<100 && $param['weight']>=90){
    		$param['weight_step'] = 5;
    	}else{
    		$param['weight_step'] = 6;
    	}

    	// income_step
    	if($param['income'] < 3000){
    		$param['income_step'] = 1;
    	}elseif($param['income']<5000 && $param['income']>=3000){
    		$param['income_step'] = 2;
    	}elseif($param['income']<7000 && $param['income']>=5000){
    		$param['income_step'] = 3;
    	}elseif($param['income']<10000 && $param['income']>=7000){
    		$param['income_step'] = 4;
    	}elseif($param['income']<15000 && $param['income']>=10000){
    		$param['income_step'] = 5;
    	}elseif($param['income']<20000 && $param['income']>=15000){
    		$param['income_step'] = 6;
    	}else{
    		$param['income_step'] = 7;
    	}
    	return $param;
    }

    /**
     * 生成统计数据
     */
    public function setData(){
    	$num = 500;
		DB::startTrans();
		try{
			for($i=0;$i<$num;$i++){
    			$data = [
    				'handsome' => rand(0,1),
    				'house' => rand(0,1),
    				'car' => rand(0,1),
    				'kind' => rand(0,1),
    				'industrious' => rand(0,1),
    				'tall_step' => rand(1,6),
    				'weight_step' => rand(1,6),
    				'female_tall_step' => rand(1,7),
    				'female_income_step' => rand(1,7),
    				'married' => rand(0,1),
    			];
    			if($data['house'] && $data['car']){
    				$data['income_step'] = rand(6,7);
    			}elseif($data['house']){
    				$data['income_step'] = rand(4,6);
    			}elseif($data['car']){
    				$data['income_step'] = rand(4,5);
    			}else{
    				$data['income_step'] = rand(1,3);
    			}
    			switch ($data['tall_step']) {
    				case '1':$data['tall'] = rand(155,159); break;
    				case '2':$data['tall'] = rand(160,164); break;
    				case '3':$data['tall'] = rand(165,169); break;
    				case '4':$data['tall'] = rand(170,174); break;
    				case '5':$data['tall'] = rand(175,179); break;
    				case '6':$data['tall'] = rand(180,188); break;
    				default: $data['tall'] = rand(155,188); break;
    			}
    			switch ($data['weight_step']) {
    				case '1':$data['weight'] = rand(57,59); break;
    				case '2':$data['weight'] = rand(60,69); break;
    				case '3':$data['weight'] = rand(70,79); break;
    				case '4':$data['weight'] = rand(80,89); break;
    				case '5':$data['weight'] = rand(90,99); break;
    				case '6':$data['weight'] = rand(100,130); break;
    				default: $data['weight'] = rand(57,130); break;
    			}
    			switch ($data['income_step']) {
    				case '1':$data['income'] = rand(2000,2999); break;
    				case '2':$data['income'] = rand(3000,4999); break;
    				case '3':$data['income'] = rand(5000,6999); break;
    				case '4':$data['income'] = rand(7000,9999); break;
    				case '5':$data['income'] = rand(10000,14999); break;
    				case '6':$data['income'] = rand(15000,19999); break;
    				case '7':$data['income'] = rand(20000,50000); break;
    				default: $data['income'] = rand(2000,50000); break;
    			}
    			DB::table('is_marry')->insert($data);
    		}
			DB::commit();
			return ['code'=>1,'msg'=>'添加成功'];
		}catch(\Exception $e){
			DB::rollback();
			return ['code'=>1,'msg'=>'添加失败:'.$e->getMessage()];
		}
    }
    /**
     * [清空数据]
     */
    public function delData(){
    	DB::startTrans();
    	try{
    		DB::table('is_marry')->where('id',"<>","-1")->delete();
    		$sql = "ALTER TABLE `is_marry` auto_increment = 1;";
    		DB::query($sql);
    		DB::commit();
			return ['code'=>1,'msg'=>'成功'];
    	}catch(\Exception $e){
    		DB::rollback();
			return ['code'=>1,'msg'=>'失败:'.$e->getMessage()];
    	}
    }
}
